package com.examiner.service.impl;

import com.examiner.bean.Do.RoleMenuDo;
import com.examiner.bean.RoleMenu;
import com.examiner.dao.RoleMenuDao;
import com.examiner.service.RoleMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    private  RoleMenuDao roleMenuDao;

    @Override
    public List<RoleMenuDo> getRoleList(String roleName) {
        List<RoleMenuDo> roleDoList = new ArrayList<>();
        RoleMenuDo roleMenuDo;
        List<RoleMenu> roleList ;
        List<RoleMenu> manager = roleMenuDao.findAllByRoleName(roleName);
        for (RoleMenu item : manager) {
            roleMenuDo = new RoleMenuDo();
            roleList =  new ArrayList<>();
            if ( item.getUId() == 0){
                roleMenuDo.setMenuId(item.getMenuId());
                roleMenuDo.setMenuName(item.getMenuName());
                roleMenuDo.setRoleName(item.getRoleName());
                roleMenuDo.setUId(item.getUId());
                for (RoleMenu roleMenu : manager) {
                    if (roleMenu.getUId().equals(item.getMenuId())){
                        roleList.add(roleMenu);
                    }
                }
                roleMenuDo.setChildren(roleList);
            }
            if (roleMenuDo.getUId() != null) roleDoList.add(roleMenuDo);
        }
        return roleDoList;
        }
}